//
// File:	ParserTokenizer.cpp
// Author:	Thomas TISSOT-DUPONT
// Login:	<thomas.tissotdupont@free.fr>
//
// Create:	02/02/2010 at 07:59:01
// Purpose:	
//
#include "Parser.hpp"

//------------------------------------------------------------------
// Constructor / Destructor
//------------------------------------------------------------------
Parser::Tokenizer::Tokenizer(const std::string &blanks /* = "" */, const std::string &separators /* = "" */)
	:	m_blanks(blanks),
		m_separators(separators)
{
}

//------------------------------------------------------------------
// Tokenize
//------------------------------------------------------------------
void Parser::Tokenizer::Tokenize(const std::string &bufferData, std::list<std::string> &listTarget)
{
	TokenizeBlanks(bufferData, listTarget);
}

void Parser::Tokenizer::TokenizeBlanks(const std::string &bufferData, std::list<std::string> &listTarget)
{
	std::size_t beg = 0;
	std::size_t end = 0;
	while ((beg = bufferData.find_first_not_of(m_blanks, end)) != std::string::npos)
	{
		end = bufferData.find_first_of(m_blanks, beg);
		std::string token = bufferData.substr(beg, end - beg);
		TokenizeSeparators(token, listTarget);
	}
}

void Parser::Tokenizer::TokenizeSeparators(const std::string &bufferData, std::list<std::string> &listTarget)
{
	std::size_t offset = bufferData.find_first_of(m_separators, 0);

	//Not found
	if (offset == std::string::npos)
	{
		listTarget.push_back(bufferData);
		return;
	}

	//Left
	if (offset > 0)
	{
		std::string left = bufferData.substr(0, offset);
		listTarget.push_back(left);
	}

	//Separator
	std::string separator(&bufferData[offset], 1);
	listTarget.push_back(separator);

	//Right
	if (offset < bufferData.length() - 1)
		TokenizeSeparators(bufferData.substr(offset + 1), listTarget);
}

//------------------------------------------------------------------
// Blanks / Keywords
//------------------------------------------------------------------
void Parser::Tokenizer::AddBlanks(const std::string &blanks)
{
	m_blanks += blanks;
}

void Parser::Tokenizer::AddSeparators(const std::string &separators)
{
	m_separators += separators;
}
